package edu.predatorwars.server.host;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import edu.predatorwars.server.engine.GameEngine;
import edu.predatorwars.shared.models.Creature;
import edu.predatorwars.shared.models.Move;
import edu.predatorwars.shared.models.Player;
import edu.predatorwars.shared.models.PlayerConnectionInfo;
import edu.predatorwars.shared.models.PlayerStatus;


public class GameHost {

	/*
	 * Acts as central host for players
	 */


	ArrayList<Player> playerList;
	ArrayList<GameInstance> gameList;
	GameEngine gameEngine;
	//Number of players in a game
	private final int numPlayers = 2;

	GameHost() {
		playerList = new ArrayList();
		gameList = new ArrayList<GameInstance>();
	}

	public void joinGame(Player joiningPlayer) {
		if (playerList.size() < (numPlayers+1)) { //Block host from adding beyond max number of players
			playerList.add(joiningPlayer);
			if (playerList.size() == numPlayers) { //Start game when there are enough players
				for (Player joinedPlayer : playerList) {
					//set all players to alive
					joinedPlayer.setPlayerStatus(PlayerStatus.ALIVE);
				}
				gameEngine = new GameEngine(playerList);
				this.startGame(playerList);
			}
		}
	}

	public void startGame(ArrayList<Player> playerList) { 
		GameInstance game = new GameInstance(playerList, this, UUID.randomUUID(), gameEngine);
		gameEngine.addGameListener(game); //The game is subscribed as a board listener to the engine
		gameList.add(game); //Host now has the game in a list of games...right now, no functionality to this
	}
	
	public void selectCharacter(Player player, Creature creature, UUID gameId) { //WEB SERVICE CLIENT CALLS
		//This should be changed in the event we allow more than one game, should search through list, based on ID
		if (gameId == null) {
			gameEngine.setCreature(player, creature.getName()); //first game instance
		} else {
			//Check list based on UUID
		}
	}
	
	public void latestUpdate(BoardPayload payload) {
		ArrayList<Player> playersToInform = (ArrayList<Player>) payload.getPlayerList();
		PlayerConnectionInfo connection = null;
		String ip = "";
		//sends message payload to clients
		for (Player player : playersToInform) {
			connection = player.getCommInfo();
			ip = connection.getIpAddress();
			//have IP, now invoke client service with payload
		}
	}
	
	public void latestUpdate(StateChangePayload payload) {
		ArrayList<Player> playersToInform = (ArrayList<Player>) payload.getPlayerList();
		PlayerConnectionInfo connection = null;
		String ip = "";
		//sends message payload to clients
		for (Player player : playersToInform) {
			connection = player.getCommInfo();
			ip = connection.getIpAddress();
			//have IP, now invoke client service with payload
		}
	}
	
	public void latestUpdate(CharacterListPayload payload, List<Player> players) {
		ArrayList<Player> playersToInform = (ArrayList<Player>) players;
		PlayerConnectionInfo connection = null;
		String ip = "";
		//sends message payload to clients
		for (Player player : playersToInform) {
			connection = player.getCommInfo();
			ip = connection.getIpAddress();
			//have IP, now invoke client service with payload
		}
	}
	
	public void move(Player player, Move move) { //WEB SERVICE
		//player makes move, redirect to game engine
		gameEngine.doMove(player, move);
	}


}